package com.zzy.utils;


import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;

import java.io.IOException;

/**
 * 查询索引
 *
 * 浏览器输入 查询全部
 * http://192.168.15.128:9200/_cat/indices?v
 *
 */
public class SearchIndex {



    public static void main(String[] args) {
        testCreateIndex();
    }

    public static void testCreateIndex() {
        HttpHost httpHost = new HttpHost("192.168.15.128", 9200, "http");
        RestClientBuilder rb = RestClient.builder(httpHost);

        rb.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
            @Override
            public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder builder) {
                return builder.setConnectTimeout(20000)// 链接超时  毫秒
                        .setSocketTimeout(20000) // 请求超时  毫秒
                        ;
            }
        });

        RestHighLevelClient client = new RestHighLevelClient(rb);

        try {
            System.out.println("打开了 elasticsearch 链接。");


            System.out.println("开始查询索引：zzyuser");
            GetIndexRequest request =   new GetIndexRequest("zzyuser");

            GetIndexResponse getIndexResponse = client.indices().get( request, RequestOptions.DEFAULT);

            System.out.println(getIndexResponse);
            System.out.println(getIndexResponse.getAliases());
            System.out.println(getIndexResponse.getMappings());
            System.out.println(getIndexResponse.getSettings());

            client.close();//关闭链接
            System.out.println("关闭了 elasticsearch 链接。");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
